package com.wip.reportingservice.daos.impls;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.jsp.jstl.fmt.LocalizationContext;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.wip.reportingservice.daos.entities.ClientsTable;
import com.wip.reportingservice.daos.entities.LogsTable;
import com.wip.reportingservice.daos.interfaces.ILogsDao;

@Repository
@Transactional
public class LogsDaoImpl implements ILogsDao{

	private static final Logger logger = LoggerFactory.getLogger(LogsDaoImpl.class);
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public void save(LogsTable page){
		logger.info("LogsDaoImpl.save");
		
		sessionFactory.getCurrentSession().save(page);
		
		
	}
 
	public void update(LogsTable page){
		logger.info("LogsDaoImpl.update");
		
		sessionFactory.getCurrentSession().update(page);
		
		
	}
 
	public void delete(LogsTable page){
		logger.info("LogsDaoImpl.delete");
		sessionFactory.getCurrentSession().delete(page);
		
	}

	public LogsTable getById(int id) {    
		logger.info("LogsDaoImpl.getById");
	    //return (PagesTable)sessionFactory.getCurrentSession().get(PagesTable.class, id); 
	    
	    final Criteria crit = sessionFactory.getCurrentSession().createCriteria(LogsTable.class);
	    
	   crit.add(Restrictions.idEq(id));
	   
	   return (LogsTable)crit.list().get(0);
	   
	   
	   
	    
	}

	@Override
	public LogsTable getByName(String name) {
		logger.info("LogsDaoImpl.getById");
	    //return (PagesTable)sessionFactory.getCurrentSession().get(PagesTable.class, id); 
	    
	    final Criteria crit = sessionFactory.getCurrentSession().createCriteria(LogsTable.class);
	    
	    String[] date=name.split("-");
	    
	    //Date logFileDate = new Date(Integer.valueOf(date[0]),Integer.valueOf(date[1]),Integer.valueOf(date[2]));
	 
	    Calendar c = Calendar.getInstance();
	    c.set(Integer.valueOf(date[0]), Integer.valueOf(date[1])-1, Integer.valueOf(date[2]));

	    
	   crit.add(Restrictions.eq("logFileDate", c.getTime()));
	   
	   List<LogsTable> results=crit.list();
	   if(!results.isEmpty())
		   return results.get(0);
	   else
		   return null;
	   
	}

	@Override
	public String test() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	@Override
	public  List<LogsTable> getAll() {
		final Criteria crit = sessionFactory.getCurrentSession().createCriteria(LogsTable.class)
			    .setProjection(Projections.projectionList()
			      .add(Projections.property("logFileDate"), "logFileDate"))
			    .setResultTransformer(Transformers.aliasToBean(LogsTable.class));

			  List<LogsTable> list = crit.list();
			  
			  return list;
	}
	
}
